# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

import MySQLdb
from twisted.enterprise import adbapi
from MySQLdb import cursors
from scrapy import signals


class MyprojectPipeline(object):
    @classmethod
    def from_settings(cls,settings):
        db_params = dict(
            host = settings['MYSQL_HOST'],
            user = settings['MYSQL_USER'],
            passwd = settings['MYSQL_PASSWD'],
            charset = settings['CHARSET'],
            db = settings['MYSQL_DBNAME'],
            use_unicode = True,
            cursorclass = cursors.DictCursor,
        )
        dbpool=adbapi.ConnectionPool('MySQLdb',**db_params)

        return cls(dbpool)

    def __init__(self,dbpool):

        self.dbpool = dbpool

    def process_item(self, item, spider):
        self.dbpool.runInteraction(self.inser_mysql,item)

    def inser_mysql(self,tx,item):
        sql = "insert  into novel2(title,url,author,sort,intro,status,img_book)" \
              " VALUES (%s,%s,%s,%s,%s,%s,%s) ON DUPLICATE KEY UPDATE url = VALUES(url) "
        if len(item['title']):
            try:
                tx.execute(sql,(item['title'],item['url'],item['author'],item['sort'],item['intro'],item['status'],item['img_book']))
            except MySQLdb.Error,e:
                print 'MySQL Error ,the title：%s'%item['title']
        else:
            print 'title is empty,I did not expect how to mark it'


